<template>
  <!-- options:指定的是数据源[树形的数据] props: 指定展示的字段和选中的值 separator分隔符-->
  <el-cascader :value="value" size="mini" :options="treeData" :props="defaultProps" separator="-" @change="changeValue" />
</template>

<script>
import { transListToTree } from '@/utils'
import { getDepts } from '@/api/deparment'

export default {
  name: 'SelectTree',
  props: {
    value: {
      type: Number,
      default: null
    }
  },
  data() {
    return {
      treeData: [],
      defaultProps: {
        label: 'name',
        value: 'id'
      }
    }
  },
  created() {
    this.getDepart()
  },
  methods: {
    async getDepart() {
      // q: 这行代码的作用是什么: 将数据转换成树形结构
      this.treeData = transListToTree(await getDepts(), 0)
    },
    changeValue(checkedIdList) {
      // console.log(arg)
      if (checkedIdList.length) {
        this.$emit('input', checkedIdList.at(-1))
      } else {
        this.$emit('input', null)
      }
    }
  }
}
</script>

<style scoped>

</style>
